import heapq
from typing import List


class Solution:
    def lastStoneWeight(self, stones: List[int]) -> int:
        heap = [0]
        # heapq 为最小堆 每次都会取出最小的数
        # 添加 -i 为最大堆，每次取出最大的数
        for i in stones:
            heapq.heappush(heap, -i)

        while len(heap) != 1:
            y = -heapq.heappop(heap)
            x = -heapq.heappop(heap)
            if x != y:
                heapq.heappush(heap, x - y)
        return -heapq.heappop(heap)